﻿using System;
using System.Runtime.Serialization;

namespace YEA.Utilities.Service.Controller
{
    public enum ServiceStatus { Paused, Stopped, InProgress, Disabled, Recovering, Initiated };
    public interface IServiceController
    {
        event EventHandler<EventArgs<ServiceStatus>> StatusChanged;
 
        void Start();

        void Stop();

        void Pause();

        void Continue();
        
        void Interpret(Exception ex);

        void Recover(Exception ex);

    }

    public class EventArgs<T> : EventArgs
    {
        public T EventData { get; private set; }
        public EventArgs(T eventData)
        {
            EventData = eventData;
        }
    }

    public static class ControllerHelper
    {
        public static event EventHandler StatusChanged;
        public static void SendStatusEvent( object sender, ServiceStatus status)
        {
            var e = new EventArgs<ServiceStatus>(status);
            if (StatusChanged != null)
                StatusChanged(sender, e);

        }
    }
    [Serializable]
    public class ServiceException : Exception
    {
        protected ServiceException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        { }
        public ServiceException(string message)
            : base(message)
        { }
        public ServiceException(string message, Exception exception)
            : base(message, exception)
        { }
        public ServiceException()
            : base()
        { }
    }

    public class NullServiceController : IServiceController
    {
        public event EventHandler<EventArgs<ServiceStatus>> StatusChanged;

        private void SendStatusEvent(ServiceStatus status)
        {
            var e = new EventArgs<ServiceStatus>(status);
            if (StatusChanged != null)
                StatusChanged(this, e);
        }

        public void Start()
        {
            Console.Out.WriteLine("service started");
            SendStatusEvent(ServiceStatus.InProgress);
        }

        public void Stop()
        {
            Console.Out.WriteLine("service stopped");
            SendStatusEvent(ServiceStatus.Stopped);
        }

        public void Pause()
        {
            Console.Out.WriteLine("service paused");
            SendStatusEvent(ServiceStatus.Paused);
        }

        public void Continue()
        {
            Console.Out.WriteLine("service continued");
            SendStatusEvent(ServiceStatus.InProgress);
        }

        public void Interpret(Exception ex)
        {
            Console.Out.WriteLine("exception diagnostic phase initiated");
            SendStatusEvent(ServiceStatus.Disabled);
        }

        public void Recover(Exception ex)
        {
            Console.Out.WriteLine("exception recovery mode initiated");
            SendStatusEvent(ServiceStatus.Recovering);
        }
    }

}
